<?php

/**
 * io actions.
 *
 * @package    essayLibrary
 * @subpackage io
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class ioActions extends sfActions
{
  
  static $modelsToIO = array('DevTask','Test','DevTaskTest','TaskLog');
  
  public function executePhing($request) {
    $dir = SettingTable::getSetting('phingDir');
    $target = SettingTable::getSetting('phingTarget');
    $command = 'phing '.$target;
    chdir($dir);
    echo "<pre>";
    echo $dir."\n<br/>";
    echo $command."\n<br/>";
    $return = '';
    system($command,$return);
    print_r($return);
    echo "</pre>";
    echo "<b>BUILD CONCLUÍDO</b>";
    die();
  }
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeExport($request)
  {
    
    $development = SettingTable::getSetting('development');
    if($development) {
      echo 'Export action may not be used in development instances.';
      die();
    }
    
    $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'exported_file'.DIRECTORY_SEPARATOR;
    $filename = $dir.DIRECTORY_SEPARATOR.'data.yml';
    
    if( file_exists($filename) ) {
      unlink($filename);
    }
    $export = new Doctrine_Data_Export_Tasks($dir);
    $export->setFormat('yml');
    $export->setModels(self::$modelsToIO);
    $export->doExportSpecifiedModels();
    
    $fp = fopen($filename, 'rb');
    header("Content-Type: text/plain; charset=UTF-8");
    header("Content-Length: " . filesize($filename));
    fpassthru($fp);
    die();
  }
  
  public function executeImport($request)
  {
    // 1 ler o arquivo exportado e gravar...
    $development = SettingTable::getSetting('development');
    if(!$development) {
      $this->getUser()->setFlash('feedback','Import action may only be used in development instances.');
      $this->redirect('tasks/index');
      die();
    }
    
    $importUrl = SettingTable::getSetting('importUrl');
    $fileData = file_get_contents($importUrl);
    //header("Content-Type: text/plain; charset=UTF-8");
    $dir = sfConfig::get('sf_data_dir').DIRECTORY_SEPARATOR.'imported_file'.DIRECTORY_SEPARATOR;
    $fileName = $dir.'dataX.yml';
    if(file_exists($fileName)) {
      unlink($fileName);
    }
    file_put_contents($fileName,$fileData);
    
    $conn = Doctrine_Manager::connection();
    $conn->beginTransaction();
    Import::$import = true;
    Doctrine::getTable('DevTaskTest')->deleteAll();
    Doctrine::getTable('TaskLog')->deleteAll();
    Doctrine::getTable('DevTask')->deleteAll();
    Doctrine::getTable('Test')->deleteAll();
    //$conn->commit();
    //die('limpou');
    try {
      $import = new Doctrine_Data_Import($fileName);
      $import->setFormat('yml');
      $import->setModels(self::$modelsToIO);
      $import->doImport(false);
      $conn->commit();
      $this->getUser()->setFlash('feedback','Data succesfully imported.');
    }
    catch(Doctrine_Exception $e) {
      $conn->rollback();
      $this->getUser()->setFlash('feedback','Error parsing received data: '.$fileName);
    }
    $this->redirect('tasks/index');
  }
}
